#include <stdio.h>
#include <stdlib.h>

#include "task_list.h"

// Adapted to my needs, based from:
// http://stackoverflow.com/questions/260915/how-can-i-create-a-dynamically-sized-array-of-structs

// Create a task list
task_list* create_task_list (int sh, int sw, int eh, int ew) {
  task_list* new_task_list = malloc(sizeof(task_list));
  if (new_task_list != NULL) {
    new_task_list->sh   = sh;
    new_task_list->sw   = sw;
    new_task_list->eh   = eh;
    new_task_list->ew   = ew;
    new_task_list->next = NULL;
  }
  return new_task_list;
}

// Delete the entire task list
void delete_task_list (task_list* old_task_list) {
  if (old_task_list != NULL) {
    // Recursively delete tasks
    delete_task_list (old_task_list->next);
  }
  free (old_task_list);
}

// Add a word to the task list
task_list* add_task (task_list* tasks, int sh, int sw, int eh, int ew) {
  task_list* new_task_list = create_task_list(sh, sw, eh, ew);
  if (new_task_list != NULL) {
    // new_task_list is now the head node
    new_task_list->next = tasks;
  }
  return new_task_list;
}

void get_array (task_list* tasks, int* arr) {
  arr[0] = tasks->sh;
  arr[1] = tasks->sw;
  arr[2] = tasks->eh;
  arr[3] = tasks->ew;
}

/*
int main (int argc, char** argv) {
  task_list* my_tasks;
  
  int* some_array = (int*) malloc (4 * sizeof(int));
  
  my_tasks = create_task_list(0,0,1,1);
  
  my_tasks = add_task (my_tasks, 2,2,3,3);
  
  task_list* iter;
  for (iter = my_tasks; iter != NULL; iter = iter->next) {
    printf("SH: %d, SW: %d, EH: %d, EW: %d\n", 
           iter->sh, iter->sw, iter->eh, iter->ew);
    get_array(iter, some_array);
    printf("SH: %d, SW: %d, EH: %d, EW: %d\n", 
            some_array[0], some_array[1], some_array[2],
            some_array[3]);
  }
  
  delete_task_list(my_tasks);
  free (some_array);
  return 0;
}
*/

